//
//  DocumentViewController.swift
//  3D Viewer
//
//  Created by orienthong on 2018/12/29.
//  Copyright © 2018 OrientHong. All rights reserved.
//

import UIKit
import SceneKit

class DocumentViewController: UIViewController {
    
    @IBOutlet weak var sceneView: SCNView!
    
    var document: UIDocument?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // Access the document
        document?.open(completionHandler: { (success) in
            if success {
                // Display the content of the document, e.g.:
                let demo = SCNScene(named: "demo.scn")!
                let scene = try! SCNScene(url: self.document!.fileURL, options: nil)
                self.sceneView.allowsCameraControl = true
                self.sceneView.scene = scene
                let pointView = self.sceneView.pointOfView
                print(scene.rootNode.childNodes)
                demo.rootNode.addChildNode(scene.rootNode)
                scene.rootNode.childNodes[0].geometry?.firstMaterial?.lightingModel = .physicallyBased
                print(scene.rootNode.childNodes[0].geometry?.materials)
                self.sceneView.scene = demo
                demo.rootNode.addChildNode(pointView!)
                
            } else {
                // Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
            }
        })
    }
    
    @IBAction func dismissDocumentViewController() {
        dismiss(animated: true) {
            self.document?.close(completionHandler: nil)
        }
    }
}
